import * as commonService from '../services/CommonCRUD';
import * as accountService from '../services/AccountDivi';
import * as datetime from '../utils/datetime'
import { message } from 'antd';
import * as common from '../utils/common'

export default {
    namespace: "CompDiviWorkFlow",
    state: {
        list: [],
        total: null,
        page: 1,
        pageSize: 10,
        isEditChange: false,
        showEdit: false,
        showSeek: false,
        showImport: false,
        record: {},
        sortor: {},
        filters: {},
        seeks: [],
        optionDatas: {},
        keyName: "ID",
        selectedRowKeys: [],
        visibleFilterName: null,
        permID:null
    },
    reducers: {
        save(state, { payload: { data: list, total, page, pageSize, sorter, filters, selectedRowKeys, seeks } }) {
            if (!sorter) {
                sorter = {}
            }
            if (!filters) {
                filters = {}
            }
            if (!seeks) {
                seeks = []
            }
            const visibleFilterName = null
            return { ...state, list, total, page, pageSize, sorter, filters, selectedRowKeys, visibleFilterName, seeks };
        },
        setSelectedRowKeys(state, { payload: { selectedRowKeys } }) {
            return { ...state, selectedRowKeys };
        },
        setRecord(state, { payload: { record } }) {
            return { ...state, record };
        },
        setKeyName(state, { payload: { keyName, permID } }) {
            return { ...state, keyName, permID };
        },
        setVisibleFilterName(state, { payload: { visibleFilterName } }) {
            return { ...state, visibleFilterName };
        },
        openEdit(state, { payload: { key } }) {
            let record = {}
            let index = -1
            if (key) {
                const { list, keyName } = state
                for (let i = 0; i < list.length; i++) {
                    if (list[i][keyName] === key) {
                        index = i;
                        break;
                    }
                }
                record = list[index]
            }
            const showEdit = true;
            const editIndex = index;
            return { ...state, showEdit, editIndex, record };
        },
        hideEdit(state) {
            const showEdit = false;
            return { ...state, showEdit };
        },
    },
    effects: {
        *fetch({ payload: { daoName, page, pageSize, sorter,filters, seeks } }, { select, call, put }) {
            try {
                const m = yield select(state => state[daoName]);
                seeks = seeks === undefined ? m.seeks : seeks;
                page = page === undefined ? m.page : page;
                pageSize = pageSize === undefined ? m.pageSize : pageSize;
                sorter = sorter === undefined ? m.sorter : sorter;
                const user = yield call(accountService.GetLogin);
                let filter ={
                    FieldName: "EMID",
                    Operator: "=",
                    FieldValues: String(user)
                }
                seeks[0].WhereItems.push(filter)
                let whereCauses = common.filtersTowhereCauses(filters)
                whereCauses = [...whereCauses, ...seeks]
                const orderItems = common.sorterToOrderItems(sorter)
                const listData = yield call(accountService.fetchComp, { daoName, page, pageSize, sorter: orderItems, filters: whereCauses, permID: m.permID });
                const selectedRowKeys = []
                if (listData) {
                    yield put({
                        type: 'save',
                        payload: {
                            data: listData.Objectlist,
                            total: listData.Count,
                            page: parseInt(page, 10),
                            pageSize: parseInt(pageSize, 10),
                            sorter, selectedRowKeys,
                            seeks
                        },
                    });
                }
            } catch (e) {
                console.log(e)
            }
        },
        *clieCompSubmitWorkFlow({ payload: {  daoName,keys } }, { call, put }) {
            let key=keys[0];
            const data =yield call(accountService.CompWorkFlow,{key});
            if(data[0].STEP===-1){
                message.info('此条申请已被拒绝，无法再次操作！如需操作请重新发起申请');
                return;
            }else if(data[0].STEP===2){
                message.info('此条申请已完成，无法再次操作！如需操作请重新发起申请');
                return;
            }
            if(data[0].STATE===1){
                message.info('此条申请已提交，无法再次操作！');
                return;
            }else{
                yield call(accountService.clieCompSubmitWorkFlow,{keys});
                yield put({ type: 'reload', payload: { daoName } });
                message.success("提交成功！", 3)
            }
        },
        *clieCompRollWorkFlow({ payload: {  daoName,keys } }, { call, put }) {
            let key=keys[0];
            const data =yield call(accountService.CompWorkFlow,{key});
            if(data[0].STEP===-1){
                message.info('此条申请已被拒绝，无需重复操作！');
                return;
            }else if(data[0].STEP===2){
                message.info('此条申请已完成，无法再次操作！如需操作请重新发起申请');
                return;
            }            
            if(data[0].STATE===1){
                message.info('此条申请已提交，无法再次操作！');
                return;
            }else{
                yield call(accountService.clieCompRollWorkFlow,{keys});
                yield put({ type: 'reload', payload: { daoName } });
                message.success("终止成功！", 3)
            }
        },
        *clieCompWorkFlow({ payload: {  daoName,istj, dataSource } }, { call, put }) {
            const result=yield call(accountService.clieCompWorkFlow,{istj, dataSource});
            if(result==="OK"){
                if(istj===""){
                    message.success("保存成功！")
                }else{
                    message.success("保存提交成功！")
                }
                yield put({ type: 'reload', payload: { daoName } });
            }else{
                message.error("保存失败！"+result)
            }
        },
        *diviworkflowgerlogin(){
            yield call(accountService.GetLogin);
        },
        *reload({ payload: { daoName } }, { put, select }) {
            const { page, sorter, pageSize, filters, record } = yield select(state => state[daoName]);
            yield put({ type: 'fetch', payload: { daoName, page, pageSize, sorter, filters } });
        },
    },
};
